package com.shoutao.website.utils.serializer;

import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Type;


/**
* @Description:
* @Param:
* @return:
* @Author: cqin
* @Date: 2019/11/4
*/
public class SerializeFactory {
    private SerializeFactory() {
    }

    /**
     *
     * @param format
     * @return
     */
    public static ISerializer getSerializer(SerializerFormatEnum format) {

        if(format == SerializerFormatEnum.JSON){
            return new JsonSerializer();
        }
        return null;
    }

    private static class JsonSerializer implements ISerializer {

        private static Gson gson = new Gson();
        @Override
        public String serialize(Object obj) {
            if (obj == null) {
                return StringUtils.EMPTY;
            }

            return gson.toJson(obj);

        }


        @Override
        public <T> T deserialize(String json, Class<T> tClass) {
            if (json.equals(StringUtils.EMPTY)) {
                return null;
            }
            return gson.fromJson(json, tClass);
        }

        @Override
        public <T> T deserializeList(String json, Type typeOfT) {
            if (json.equals(StringUtils.EMPTY)) {
                return null;
            }
            return gson.fromJson(json, typeOfT);
        }

    }

    /**
     * 枚举，暂时不支持xml
     */
    public enum SerializerFormatEnum{
        JSON
        //暂时不支持xml
    }
}
